import pygame
import level
import cfg
import obj


'''
    显示开始界面：bgm、背景图、游戏标题、开始游戏
    加载鼠标准星
    点击事件的判断、点击的音效
    点击后break出来，跳转到level1
    向level1传递screen、鼠标准星
'''
def start():
    # 游戏初始化
    pygame.init()
    # 创建屏幕
    screen = pygame.display.set_mode(cfg.SCREEN_SIZE)
    # 标题
    pygame.display.set_caption('与小胶囊一起抗击病毒')
    # 创建准星
    star = obj.Star()
    # 背景
    bg_img = pygame.image.load(cfg.START_BG_IMG_PATH)
    bg_img = pygame.transform.scale(bg_img,cfg.START_BG_IMG_SIZE)
    bg_img_rect = bg_img.get_rect()
    # bgm
    pygame.mixer.music.load(cfg.START_BGM)
    pygame.mixer.music.play(-1)

    # 开始按钮
    font_button = pygame.font.Font(cfg.FONT_BUTTON_PATH,cfg.FONT_BUTTON_SIZE)
    text_button = font_button.render("开始",True,cfg.FONT_BUTTON_COLOR)
    text_button_rect = text_button.get_rect()
    text_button_rect.centerx,text_button_rect.centery = cfg.SCREEN_SIZE[0]/2,cfg.SCREEN_SIZE[1]/2 + 100

    # 标题
    
    font_title = pygame.font.Font(cfg.FONT_TITLE_PATH,cfg.FONT_TITLE_SIZE)
    text_title = font_title.render("与小胶囊一起消灭病毒！",True,cfg.FONT_TITLE_COLOR)
    text_title_rect = text_title.get_rect()
    text_title_rect.centerx,text_title_rect.centery = cfg.SCREEN_SIZE[0]/2,cfg.SCREEN_SIZE[1]/2 - 100
    title_bg = pygame.Surface((cfg.SCREEN_SIZE[0],text_title.get_height()+10), pygame.SRCALPHA)

    # 游戏说明
    font_intro = pygame.font.Font(cfg.FONT_TITLE_PATH,20)
    text_intro = font_intro.render("游戏说明：",True,cfg.FONT_BUTTON_COLOR)
    text_intro_rect = text_intro.get_rect()
    text_intro_rect.x = 3/4 * cfg.SCREEN_SIZE[0] - 20
    text_intro_rect.y = cfg.SCREEN_SIZE[1] - 120
    text_intro_1 = font_intro.render("按WSAD或方向键控制胶囊移动",True,cfg.FONT_BUTTON_COLOR)
    text_intro_1_rect = text_intro.get_rect()
    text_intro_1_rect.x = 3/4 * cfg.SCREEN_SIZE[0] - 20
    text_intro_1_rect.y = cfg.SCREEN_SIZE[1] - 90
    text_intro_2 = font_intro.render("使用鼠标移动准星，左键射击",True,cfg.FONT_BUTTON_COLOR)
    text_intro_2_rect = text_intro.get_rect()
    text_intro_2_rect.x = 3/4 * cfg.SCREEN_SIZE[0] - 20
    text_intro_2_rect.y = cfg.SCREEN_SIZE[1] - 60
    text_intro_3 = font_intro.render("随机出现的十字回旋镖破坏力惊人哦",True,cfg.FONT_BUTTON_COLOR)
    text_intro_3_rect = text_intro.get_rect()
    text_intro_3_rect.x = 3/4 * cfg.SCREEN_SIZE[0] - 20
    text_intro_3_rect.y = cfg.SCREEN_SIZE[1] - 30

    
    # 条件满足进入level1的标志
    flag = False
    while True:
        screen.blit(bg_img,bg_img_rect)
           # per-pixel alpha
        title_bg.fill((0,0,0,170))                         # notice the alpha value in the color
        screen.blit(title_bg, (0,text_title_rect.y-5))
        pygame.draw.rect(screen,cfg.BUTTON_COLOR,(cfg.SCREEN_SIZE[0]/2-cfg.BUTTON_SIZE[0]/2,cfg.SCREEN_SIZE[1]/2-cfg.BUTTON_SIZE[1]/2+100,cfg.BUTTON_SIZE[0],cfg.BUTTON_SIZE[1]))

        screen.blit(text_button,text_button_rect)
        screen.blit(text_title,text_title_rect)

        screen.blit(text_intro,text_intro_rect)
        screen.blit(text_intro_1,text_intro_1_rect)
        screen.blit(text_intro_2,text_intro_2_rect)
        screen.blit(text_intro_3,text_intro_3_rect)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
            elif event.type == pygame.MOUSEMOTION:
                star.set_position(pygame.mouse.get_pos())
            elif event.type == pygame.MOUSEBUTTONDOWN:
                if event.button == 1 and text_button_rect.collidepoint(pygame.mouse.get_pos()):
                    flag = True
        star.draw(screen)
        pygame.display.update()
        if flag:
            break
    pygame.mixer.music.stop()
    # success(screen,star)
    level.level1(screen,star)

def end(screen,star):
    '''
    bgm再来一次、退出游戏
    再来一次跳转到level1
    退出游戏就退出
    '''
    # bgm
    pygame.mixer.music.load(cfg.FAIL_BGM)
    pygame.mixer.music.play()

    # 按钮
    font_button = pygame.font.Font(cfg.FONT_BUTTON_PATH,cfg.FONT_BUTTON_SIZE)
    text_button = font_button.render("重新开始",True,cfg.FONT_BUTTON_COLOR)
    text_button_rect = text_button.get_rect()
    text_button_rect.centerx,text_button_rect.centery = cfg.SCREEN_SIZE[0]/2,cfg.SCREEN_SIZE[1]/2 + 100

    # 标题
    font_title = pygame.font.Font(cfg.FONT_TITLE_PATH,cfg.FONT_TITLE_SIZE)
    text_title = font_title.render("败北",True,cfg.FONT_TITLE_COLOR)
    text_title_rect = text_title.get_rect()
    text_title_rect.centerx,text_title_rect.centery = cfg.SCREEN_SIZE[0]/2,cfg.SCREEN_SIZE[1]/2 - 100
    
    # 进入level1的标志
    flag = False
    while True:
        screen.fill((0,0,0))
        pygame.draw.rect(screen,cfg.BUTTON_COLOR,(cfg.SCREEN_SIZE[0]/2-cfg.BUTTON_SIZE[0]/2-50,cfg.SCREEN_SIZE[1]/2-cfg.BUTTON_SIZE[1]/2+100,cfg.BUTTON_SIZE[0]+100,cfg.BUTTON_SIZE[1]))
        screen.blit(text_button,text_button_rect)
        screen.blit(text_title,text_title_rect)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
            elif event.type == pygame.MOUSEMOTION:
                star.set_position(pygame.mouse.get_pos())
            elif event.type == pygame.MOUSEBUTTONDOWN:
                if event.button == 1 and text_button_rect.collidepoint(pygame.mouse.get_pos()):
                    flag = True
        star.draw(screen)
        pygame.display.update()
        if flag:
            break
    pygame.mixer.music.stop()
    level.level1(screen,star)
    
# 对医务人员的致敬，滚动图片即可
def success(screen,star):
    pygame.mixer.music.load(cfg.APP_BGM)
    pygame.mixer.music.play(-1)
    pic = pygame.image.load(cfg.APP_IMG)
    pic = pygame.transform.scale(pic,cfg.APP_SIZE)
    pic_rect = pic.get_rect()
    pic_rect.centerx = cfg.SCREEN_SIZE[0]/2
    pic_rect.y = cfg.SCREEN_SIZE[1]
    clock = pygame.time.Clock()
    while True:
        screen.fill((0,0,0))
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
            elif event.type == pygame.MOUSEMOTION:
                star.set_position(pygame.mouse.get_pos())
        screen.blit(pic,pic_rect)
        star.draw(screen)
        if pic_rect.y >= -3000:
            pic_rect.y -= 2
        pygame.display.update()
        clock.tick(60)
